import random

from moviepy.editor import *
from moviepy.video.compositing.transitions import crossfadeout
from moviepy.video.fx.fadein import fadein
from moviepy.video.fx.fadeout import fadeout

input_audio = r'D:\shq\file\music-new\凡人修仙传-凡人.mp3'
out_audio = r'D:\shq\file\music-new\凡人修仙传-凡人-简短.mp3'
input_1 = r'E:\songhq\video\transitions\zhongkong.mp4'
input_2 = r'E:\songhq\video\transitions\jiashishi.mp4'
outPath = r'E:\songhq\video\transitions\transition-2.mp4'
picturePath = r'E:\songhq\video\transitions\pic_out.mp4'
image_files = [r"D:\shq\code\moviepy-video\video\image1.jpg", r"D:\shq\code\moviepy-video\video\image2.jpg"]

if __name__ == '__main__':
    video = AudioFileClip(input_audio)
    print("视频的长度为：" + str(video.duration))

    video = video.subclip(0, 240 + 13)

    video.write_audiofile(filename=out_audio)
    # 截取图片
    # video.save_frame("image1.jpg", t=3)
    # video2 = VideoFileClip(input_2)
    # video2.save_frame("image2.jpg", t=0)
    # clips = [ImageSequenceClip([image_files[0]], durations=[1]), ImageSequenceClip([image_files[1]], durations=[1])]
    # 图片的大小一样
    # clips = concatenate_videoclips(clips[0].fadeout(1),)
    # clips_out = fadeout(clips[0], 1)
    # clips_out = fadeout(video, 1)
    # clips_in = fadein(clips[1], 1)
    #
    # image_clips = ImageClip(image_files[0])
    # # image_clips.
    # clips_out.write_videofile(filename=picturePath, fps=24)
    # print(clips)
    # # concatenate 中的method选择compose可以实现大小不同的视频组合
    # final_clip = concatenate_videoclips([clips_out, video2], method='compose')
    # final_clip.write_videofile(filename=outPath, fps=24)


# import random
# from moviepy.editor import ImageSequenceClip, concatenate

# 图片列表
# image_files = ["image1.jpg", "image2.jpg", "image3.jpg", "image4.jpg"]
#
# # 定义随机过场特效函数
def add_random_transition(clip1, clip2):
    transitions = [
        lambda c1, c2: concatenate([c1, c2.set_start(c2.duration)]),  # 直接连接
        lambda c1, c2: concatenate([c1.fadeout(1), c2.fadein(1)]),  # 淡入淡出
        lambda c1, c2: concatenate([c1.crossfadein(1), c2.crossfadeout(1)]),  # 交叉淡入淡出
        # 添加更多的过场特效函数
    ]
    transition_func = random.choice(transitions)
    return transition_func(clip1, clip2)
#
# # 加载图片序列剪辑
# clips = [ImageSequenceClip([image], durations=3) for image in image_files]
#
# # 添加随机过场特效
# transitions = [add_random_transition(clip1, clip2) for clip1, clip2 in zip(clips, clips[1:])]
#
# # 拼接剪辑
# final_clip = concatenate([clips[0]] + transitions)
#
# # 保存输出视频文件
# final_clip.write_videofile("output.mp4", fps=24)
